{% extends "base.html" %}

{% block title %}订单列表 - 妙趣拾光锦盒管理系统{% endblock %}

{% block content %}
<div class="row mb-4">
    <div class="col-12 d-flex justify-content-between align-items-center">
        <h2 class="mb-0"><i class="fas fa-shopping-cart me-2"></i>订单列表</h2>
    </div>
</div>

<!-- 筛选器 -->
<div class="card mb-4 shadow-sm">
    <div class="card-header bg-light">
        <h5 class="mb-0"><i class="fas fa-filter me-2"></i>筛选条件</h5>
    </div>
    <div class="card-body">
        <form method="GET" action="{{ url_for('order.list') }}">
            <div class="row">
                <div class="col-md-4 mb-2">
                    <label for="order_no" class="form-label">订单编号</label>
                    <input type="text" class="form-control" id="order_no" name="order_no" value="{{ request.args.get('order_no', '') }}">
                </div>
                <div class="col-md-4 mb-2">
                    <label for="status" class="form-label">订单状态</label>
                    <select class="form-select" id="status" name="status">
                        <option value="">全部状态</option>
                        <option value="1" {% if request.args.get('status') == '1' %}selected{% endif %}>待支付</option>
                        <option value="2" {% if request.args.get('status') == '2' %}selected{% endif %}>待发货</option>
                        <option value="3" {% if request.args.get('status') == '3' %}selected{% endif %}>已发货</option>
                        <option value="4" {% if request.args.get('status') == '4' %}selected{% endif %}>已完成</option>
                        <option value="5" {% if request.args.get('status') == '5' %}selected{% endif %}>已取消</option>
                    </select>
                </div>
                <div class="col-md-4 mb-2">
                    <label for="username" class="form-label">用户名</label>
                    <input type="text" class="form-control" id="username" name="username" value="{{ request.args.get('username', '') }}">
                </div>
            </div>
            <div class="d-flex justify-content-end mt-3">
                <button type="submit" class="btn btn-primary">
                    <i class="fas fa-search me-1"></i>查询
                </button>
                <a href="{{ url_for('order.list') }}" class="btn btn-secondary ms-2">
                    <i class="fas fa-redo me-1"></i>重置
                </a>
            </div>
        </form>
    </div>
</div>

<!-- 订单列表 -->
<div class="card shadow-sm">
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-hover table-sortable">
                <thead>
                    <tr>
                        <th data-sort="id" scope="col">#</th>
                        <th data-sort="order_no" scope="col">订单编号</th>
                        <th data-sort="user" scope="col">用户</th>
                        <th data-sort="amount" scope="col">金额</th>
                        <th data-sort="date" scope="col">下单时间</th>
                        <th data-sort="status" scope="col">状态</th>
                        <th scope="col">操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for order in orders %}
                    <tr>
                        <td data-id="{{ order.order_id }}">{{ order.order_id }}</td>
                        <td>{{ order.order_no }}</td>
                        <td>{{ order.user.username if order.user else '未知用户' }}</td>
                        <td data-amount="{{ order.total_amount }}">
                            <span class="text-danger">¥{{ "%.2f"|format(order.total_amount) }}</span>
                        </td>
                        <td data-date="{{ order.create_time.strftime('%Y%m%d%H%M%S') if order.create_time else '' }}">
                            {{ order.create_time.strftime('%Y-%m-%d %H:%M:%S') if order.create_time else '未知' }}
                        </td>
                        <td data-status="{{ order.status }}">
                            {% if order.status == 1 %}
                            <span class="badge bg-warning text-dark">待支付</span>
                            {% elif order.status == 2 %}
                            <span class="badge bg-info">待发货</span>
                            {% elif order.status == 3 %}
                            <span class="badge bg-primary">已发货</span>
                            {% elif order.status == 4 %}
                            <span class="badge bg-success">已完成</span>
                            {% elif order.status == 5 %}
                            <span class="badge bg-secondary">已取消</span>
                            {% else %}
                            <span class="badge bg-secondary">未知状态</span>
                            {% endif %}
                        </td>
                        <td>
                            <div class="btn-group">
                                <a href="{{ url_for('order.detail', order_id=order.order_id) }}" class="btn btn-sm btn-info">
                                    <i class="fas fa-info-circle"></i>
                                </a>
                                {% if order.status == 1 %}
                                <a href="{{ url_for('order.update_status', order_id=order.order_id, status=2) }}" class="btn btn-sm btn-success" title="设为已支付">
                                    <i class="fas fa-check"></i>
                                </a>
                                <a href="{{ url_for('order.update_status', order_id=order.order_id, status=5) }}" class="btn btn-sm btn-danger" title="取消订单">
                                    <i class="fas fa-times"></i>
                                </a>
                                {% elif order.status == 2 %}
                                <a href="{{ url_for('order.update_status', order_id=order.order_id, status=3) }}" class="btn btn-sm btn-primary" title="设为已发货">
                                    <i class="fas fa-shipping-fast"></i>
                                </a>
                                <a href="{{ url_for('order.update_status', order_id=order.order_id, status=5) }}" class="btn btn-sm btn-danger" title="取消订单">
                                    <i class="fas fa-times"></i>
                                </a>
                                {% elif order.status == 3 %}
                                <a href="{{ url_for('order.update_status', order_id=order.order_id, status=4) }}" class="btn btn-sm btn-success" title="设为已完成">
                                    <i class="fas fa-check-double"></i>
                                </a>
                                <a href="{{ url_for('order.update_status', order_id=order.order_id, status=5) }}" class="btn btn-sm btn-danger" title="取消订单">
                                    <i class="fas fa-times"></i>
                                </a>
                                {% endif %}
                            </div>
                        </td>
                    </tr>
                    {% else %}
                    <tr>
                        <td colspan="7" class="text-center py-4">
                            <div class="text-muted">
                                <i class="fas fa-shopping-cart fa-3x mb-3"></i>
                                <p>暂无订单数据</p>
                            </div>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>
{% endblock %}
